function quantity_modify(item_id) {
    var quantity = $("#quantity_"+item_id).val();
    var submit = "Update";
    $.post("/modify_cart/", { 'quantity': quantity, 'item_id': item_id, 'submit': submit},function(data) {
        if (data['single_item_total']!=0) {
            $("#one-item-total-"+item_id).html("<font>"+parseFloat(data['single_item_total']).toFixed(1)+" €</font>");
        }
        else {
            $("#item-info-"+item_id).remove();
        }
        if (data['all_items_total']==0) {
            $("#check-area").remove();
        }
        $("#cart-subtotal").text("Cart Subtotal: "+parseFloat(data['all_items_total']).toFixed(1)+" €");
        $("#items_amount").text(data['items_amount']);
        $("#cart_url").text('My Cart('+data['items_amount']+')');
    });
    //return false;
}

function remove_item(item_id) {
    if (confirm("Remove this from cart?") == true) {
        var submit = "Remove";
        $.post("/modify_cart/", { 'item_id': item_id, 'submit': submit},function(data) {
            $("#item-info-"+item_id).remove();
            $("#cart-subtotal").text("Cart Subtotal: "+parseFloat(data['all_items_total']).toFixed(1)+" €");
            $("#items_amount").text(data['items_amount']);
            $("#cart_url").text('My Cart('+data['items_amount']+')');
            if (data['items_amount']==0) {
                $("#shopping_cart tbody").html('<tr>'
			+'<td colspan="14" style="height:30px;" id="cart-empty">'
			+    'Your cart is empty.'
			+'</td>'
		    +'</tr>');
                $("#check-area").remove();
            }
        });
        //return false;
    }
}

function add_to_favourite(product_id) {
    var url = "/favourite/"+product_id+"/";
    if ($("#product_id_"+product_id).val()!=undefined) {
        alert("This product is already stored up!");
    }
    else {
        $("#outline-div").load(url);
    }
    //return false;
}

function remove_from_favourite(favourite_product_id) {
    if (confirm("Remove this from my favourite goods?") == true) {
        var url = "/favourite/delete/";
        $.post(url, {'favourite_product_id':favourite_product_id}, function(data){
            if (data['success']==true) {
                $("#favourite_good_"+favourite_product_id).remove();
            }
        });
    }
    //return false;
}

function add_to_cart_submit() {
    product_id = $("#product-id").val();
    quantity = $("#buy-amount-text").val();
    token = $("#csrfmiddlewaretoken").val();
    url = "/products/add_to_cart/";
    $.post(url,{"product-id": product_id, "buy-amount": quantity, "csrfmiddlewaretoken": token}, function(data){
        $("#items_amount").text(data['items_amount']);
	$("#goods_amount").text(data['items_amount']);
	$("#total_price").text(data['sub_total']);
        $("#cart_url").text('My Cart('+data['items_amount']+')');
    });
}

function favourite_to_cart(product_id) {
    if (document.getElementById("product-id-"+product_id)==undefined){
        quantity = 1
        url = "/products/add_to_cart/";
        $.post(url,{"product-id": product_id, "buy-amount": quantity}, function(data){
            $("#shopping_cart tbody tr:last").after('<tr id="item-info-'+data['cart_id']+'" align="center">'
                            +'<td align="center">'
                            +    '<img src="/'+data['image']+'" />'
                            +'</td>'
			    +'<td colspan="4">'
				+'<a href="/products/'+data['product_id']+'" class="cart" style="color: blue;">'
				    +'"'+data['name']+'"'
				+'</a>'
			    +'</td>'
			    +'<td colspan="5" align="center"><font>'+parseFloat(data['price']).toFixed(1)+'&nbsp;€</font></td>'
			    +'<td>'
				+'<form method="post" action="javascript:quantity_modify('+data['cart_id']+');" class="cart">'
				    +'<input type="text" name="quantity" value="'+data['quantity']+'" id="quantity_'+data['cart_id']+'" size="2" class="quantity" maxlength="5" style="border:1px solid #999; width:30px; height:25px; margin-top:6px;" />'
                                    +'<input type="image" src="/site_media/images/cart_add.jpg" style="width:40px; height:40px; position: absolute;"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
                                +'</form>'
                            +'</td>'
			    +'<td>'
				+'<form method="post" action="javascript:remove_item('+data['cart_id']+')" class="cart">'
				    +'<input type="image" src="/site_media/images/cart_remove.jpg" />'
				+'</form>'
			    +'</td>'
                            +'<td>'
                                +'<a href="javascript:void(0)" onclick="add_to_favourite('+data['product_id']+')" style="color: blue;">Click to store</a>'
                            +'</td>'
			    +'<td class="table-right" id="one-item-total-'+data['cart_id']+'" align="center">'
                                +'<font>'+parseFloat(data['total']).toFixed(1)+'&nbsp;€</font>'
                                +'<input type="hidden" value="'+data['product_id']+'" id="product-id-'+data['product_id']+'" />'
                            +'</td>'
			+'</tr>');
            if (document.getElementById("check-area")==undefined && data['sub_total']>0) {
                $("#shopping_cart tfoot tr:last").after('<tr id="check-area">'
                                +'<th colspan="15" align="right">'
                                +'<form method="POST" action=".">'
                                    +'<input type="hidden" name="submit" value="Checkout" />'
                                    +'<a href="/cart/checkout/"><img src="/site_media/images/icon_checkout.gif" alt="Checkout" /></a>'
                                +'</form>'
                            +'</th>');
            }
            $("#cart-subtotal").text("Cart Subtotal: "+parseFloat(data['sub_total']).toFixed(1)+" €");
            $("#items_amount").text(data['items_amount']);
            $("#cart_url").text('My Cart('+data['items_amount']+')');
            if (document.getElementById("cart-empty")!=undefined) {
                $("#cart-empty").remove();
            }
        });
    }
    else {
        alert("This product is already in your cart!");
    }
}

function user_login_panel(comment_id) {
    $("#login_link").fancybox({
        onStart: function() {$("#login_panel").css("display","block");},
	onClosed: function () {
	    $("#login_panel").css("display","none");
	    url = window.location.pathname;
	    if (url.length==6) {
		$("#outline-div").load("/cart/");
	    }
	    else {
		$("#outline-div").load("/products/comment/reply/"+comment_id+"/");
	    }
	}
    });
    $("#login_link").trigger('click');
}

function user_login() {
    if ($("#id_username").val()=='' || $("#id_password").val()=='') {
        alert("Please input username and password!");
    }else {
        url = "/user/check/login/";
        data={
            'username': $("#id_username").val(),
            'password': $("#id_password").val(),
	    "csrfmiddlewaretoken": $("#csrfmiddlewaretoken").val()
        }
        $.post(url,data,function(result){
            if (result['success']==true) {
                $.fancybox.close();
            }
            else {
                alert(result['error_message']);
            }
        });
    }
}

function show_success_info() {
    $("#success_info_trigger").fancybox({
        onStart: function() {add_to_cart_submit();$("#success_info_box").css("display","block");},
	onClosed: function () { $("#success_info_box").css("display","none");}
    });
    $("#success_info_trigger").trigger('click');
}